using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WPCoco.UI
{
    public class UIButton : Sprite, IResponder
    {
        public UIButton(TextureFrame normalFrame, TextureFrame activeFrame)
            : base(normalFrame)
        {
            NormalFrame = normalFrame; ActiveFrame = activeFrame;
            Enabled = true;
        }

        public UIButton(TextureFrame frame) : this(frame, frame) { }

        public UIButton(Texture2D normalTexture, Texture2D activeTexture)
            : this(TextureFrame.CreateFromTexture(normalTexture), TextureFrame.CreateFromTexture(activeTexture))
        {

        }

        public UIButton(Texture2D texture) : this(texture, texture) { }

        protected TextureFrame NormalFrame { get; set; }

        protected TextureFrame ActiveFrame { get; set; }

        public event EventHandler Action;

        protected void RaiseAction() 
        {
            if (Action == null) return;
            Action(this, EventArgs.Empty);
        }

        bool actived = false;

        public bool Actived
        {
            get { return actived; }
            set 
            {
                if (actived == value) return;
                actived = value;
                if (actived)
                    SetTextureFrame(ActiveFrame);
                else
                    SetTextureFrame(NormalFrame);
            }
        }

        public bool Enabled { get; set; }

        Vector2 lastPointerPosition, startPointerPosition;

        const float moveTolerance = 5;

        public bool PointerDown(Vector2 position)
        {
            if (!IsDrawed || !Enabled) return false;
            lastPointerPosition = position; startPointerPosition = position;
            Matrix m = MatrixHelper.GetViewWorldMatrix(this);
            Vector2.Transform(ref position, ref m, out position);
            RectangleF rect = new RectangleF(0, 0, Width, Height);
            Actived = rect.Contains(position);
            return Actived;
        }

        public void PointerMove(Vector2 position)
        {
            if (!IsDrawed || !Enabled) return;
            lastPointerPosition = position;
            Matrix m = MatrixHelper.GetViewWorldMatrix(this);
            Vector2.Transform(ref position, ref m, out position);
            RectangleF rect = new RectangleF(0, 0, Width, Height);
            Actived = rect.Contains(position);
        }

        public void PointerUp(Vector2 position)
        {
            if (!IsDrawed || !Enabled) return;
            float dif = (startPointerPosition - lastPointerPosition).Length();
            if (dif > moveTolerance) 
            {
                Actived = false;
                return;
            }
            position = lastPointerPosition;
            Matrix m = MatrixHelper.GetViewWorldMatrix(this);
            Vector2.Transform(ref position, ref m, out position);
            RectangleF rect = new RectangleF(0, 0, Width, Height);
            bool check = rect.Contains(position);
            Actived = false;
            if (check) RaiseAction();
        }
    }
}
